/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.citi.dahg.cve.controller.service.impl;

import com.citi.dahg.cve.controller.model.Area;
import com.citi.dahg.cve.controller.model.Pais;
import com.citi.dahg.cve.controller.model.Recurso;
import com.citi.dahg.cve.controller.service.BasicService;
import com.citi.dahg.cve.controller.service.local.AreaService;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

/**
 *
 * @author dh90940
 */
@Stateless
public class AreaServiceImpl extends BasicService<Area> implements AreaService {

    @EJB
    private PaisServiceImpl paisService;
    
    @Override
    public List<Pais> getAllPaises() {
        return paisService.getAll();
    }

    @Override
    public List<Recurso> getSupervisores(Area area) {
        String consulta = "select r from Recurso r where r.isSupervisor=:is_supervisor and r.area=:area";
        Query query = getEntityManager().createQuery(consulta)
                .setParameter("is_supervisor", true)
                .setParameter("area", area);
        
        List<Recurso> lista = new ArrayList<Recurso>();
        
        lista.addAll(query.getResultList());
        
        return lista;
    }

    @Override
    protected Class<Area> getClazz() {
        return Area.class;
    }

    @Override
    protected String queryGetAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Area findByName(String nombre) {
        String consulta = "select a from Area a where trim(upper(a.descripcion)) = trim(upper(:nombre))";
        Query query = getEntityManager().createQuery(consulta)
                .setParameter("nombre", nombre);
        Area area = null;
        try {
            area = (Area) query.getSingleResult();
        }
        catch (NoResultException ex) {
            return area;
        }
        return area;
    }

    @Override
    public List<Area> getAreaByPais(Pais p) {
        List<Area> lista = new ArrayList<Area>();
        String consulta="select a from Area a where a.pais=:pais";
        Query query = getEntityManager().createQuery(consulta)
                .setParameter("pais", p);
        lista.addAll(query.getResultList());
        return lista;
               
    }

    @Override
    public List<Recurso> getSubalternos(Recurso supervisor) {
        List<Recurso> lista = new ArrayList<Recurso>();
        String consulta = "select r from Recurso r where r.supervisor=:supervisor";
        Query query = getEntityManager().createQuery(consulta)
                .setParameter("supervisor", supervisor);
        
        lista.addAll(query.getResultList());
        
        return lista;
    }

    @Override
    public Area getAreaByPaisAndDescripction(Pais pais, String description) {
        String consulta = "select a from Area a where a.pais=:pais and trim(upper(a.descripcion))=trim(upper(:descripcion))";
        Query query = getEntityManager().createQuery(consulta)
                .setParameter("pais", pais)
                .setParameter("descripcion", description);
        Area area = null;
        try {
            area = (Area) query.getSingleResult();
        }
        catch (NoResultException ex) {
            return area;
        }
        return area;
    }
    
}
